package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.FollowBean;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;

/**
 * Created by 廉冲-android开发 on 2017/5/17.
 */
public class Follow_MSGAdapter extends BaseAdapter {
    List<FollowBean.ListEntity> mList = new ArrayList<>();
    LayoutInflater inflater;
    Context context;
    OnItemClickListener mOnItemClickListener;
    /**
     * true：关注
     * false：粉丝
     */
    public boolean bool_follow = false;

    public Follow_MSGAdapter() {
    }

    public Follow_MSGAdapter(List<FollowBean.ListEntity> mList, Context context) {
        this.mList = mList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onGuanzhuClick(String followCode, int position);
    }

    public void setList(List<FollowBean.ListEntity> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_follow_usermsg, null);
            viewHolder = new ViewHolder();

            viewHolder.textView_follow_nickname = (TextView) convertView.findViewById(R.id.textView_follow_nickname);
            viewHolder.textView_follow_fansnum = (TextView) convertView.findViewById(R.id.textView_follow_fansnum);
            viewHolder.textView_follow_msg = (TextView) convertView.findViewById(R.id.textView_follow_msg);
            viewHolder.imageView_follow_headerimg = (ImageView) convertView.findViewById(R.id.imageView_follow_headerimg);
            viewHolder.imageView_follow_msg = (ImageView) convertView.findViewById(R.id.imageView_follow_msg);
            viewHolder.linearLayout_follow_msg = (LinearLayout) convertView.findViewById(R.id.linearLayout_follow_msg);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final FollowBean.ListEntity listEntity = mList.get(position);

        viewHolder.textView_follow_nickname.setText(listEntity.getNick_name());
        viewHolder.imageView_follow_headerimg.setImageURI(Uri.parse(listEntity.getAvatar()));
        String nameString = context.getResources().getString(bool_follow ? R.string.follow_msg_guanzhu : R.string.follow_msg_fensi);
        String nameFinal = String.format(nameString, listEntity.getAfollowsum()+"");
        viewHolder.textView_follow_fansnum.setText(nameFinal);

        if ((bool_follow ? listEntity.getFollowcode() : listEntity.getUsercode()).equals(ForPreference.getString(ForContents.USER_CODE))){
            viewHolder.imageView_follow_msg.setVisibility(View.GONE);
            viewHolder.textView_follow_msg.setVisibility(View.GONE);
            viewHolder.linearLayout_follow_msg.setBackgroundResource(R.color.color_ffffff);
        }else {
            viewHolder.imageView_follow_msg.setVisibility(listEntity.getAfollow() == 2 ? View.VISIBLE : View.GONE);
            viewHolder.textView_follow_msg.setTextColor(listEntity.getAfollow() == 2 ?
                    context.getResources().getColor(R.color.color_follow_ff3b30) : context.getResources().getColor(R.color.color_follow_868686));
            viewHolder.linearLayout_follow_msg.setBackgroundResource(listEntity.getAfollow() == 2 ?
                    R.color.color_ffffff : R.drawable.shape_follow_btn_bg);
            switch (listEntity.getAfollow()){
                case 1:
                    viewHolder.textView_follow_msg.setText( R.string.momcircle_guanzhu_1);
                    break;
                case 2:
                    viewHolder.textView_follow_msg.setText( R.string.momcircle_guanzhu_2);
                    break;
                case 3:
                    viewHolder.textView_follow_msg.setText( R.string.momcircle_guanzhu_3);
                    break;
            }
        }

        viewHolder.linearLayout_follow_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!(bool_follow ? listEntity.getFollowcode() : listEntity.getUsercode()).equals(ForPreference.getString(ForContents.USER_CODE))){
                    String code = bool_follow ? listEntity.getFollowcode() : listEntity.getUsercode() ;
                    mOnItemClickListener.onGuanzhuClick(code , position);
                }else {
                    ForToast.showToast("不能关注自己");
                }
            }
        });
        return convertView;
    }

    public class ViewHolder {
        TextView textView_follow_nickname;
        TextView textView_follow_fansnum;
        TextView textView_follow_msg;
        ImageView imageView_follow_headerimg;
        ImageView imageView_follow_msg;
        LinearLayout linearLayout_follow_msg;
    }
}
